﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.IO;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Ym00inq")]
    public class Ym00inqController : ApiController
    {

        [HttpPost]
        [Route("PostYm00inq_inq")]


        public System.Data.DataSet PostYm00inq_inq([FromBody]  JObject obj)
        {
            EI.EIInfo inBlock = new EI.EIInfo();

            string OUT_STOCK_NO= obj["OUT_STOCK_NO"] == null ? " " : obj["OUT_STOCK_NO"].ToString();
            string OUT_STOCK_APPLY_NO = obj["OUT_STOCK_APPLY_NO"] == null ? " " : obj["OUT_STOCK_APPLY_NO"].ToString();



            string BILL_STATUS = obj["BILL_STATUS"] == null ? " " : obj["BILL_STATUS"].ToString();

            
            string REC_CREATE_TIME = obj["REC_CREATE_TIME"] == null ? " " : obj["REC_CREATE_TIME"].ToString();
            string REC_REVISE_TIME = obj["REC_REVISE_TIME"] == null ? " " : obj["REC_REVISE_TIME"].ToString();


            string page_no = obj["PAGE_NO"] == null ? " " : obj["PAGE_NO"].ToString();
            string page_size = obj["PAGE_SIZE"] == null ? " " : obj["PAGE_SIZE"].ToString();
            string type_inq = obj["TYPE_INQ"] == null ? " " : obj["TYPE_INQ"].ToString();

            string userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string username = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();

            string formname = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();



           
            inBlock.Tables[0].Columns.Add("OUT_STOCK_NO"); 
            inBlock.Tables[0].Columns.Add("OUT_STOCK_APPLY_NO");  

            inBlock.Tables[0].Columns.Add("BILL_STATUS"); 
            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");



            inBlock.Tables[0].Rows.Add();
            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid = userid;
            inBlock.sys_info.formname = userid;
            inBlock.sys_info.formname = formname;//Ym00inq封锁，否则为判定查询记录



            inBlock.Tables[0].Rows[0]["OUT_STOCK_NO"] = OUT_STOCK_NO;
            inBlock.Tables[0].Rows[0]["OUT_STOCK_APPLY_NO"] = OUT_STOCK_APPLY_NO;


            
            inBlock.Tables[0].Rows[0]["BILL_STATUS"] = BILL_STATUS; 
         

            //1表示高级查询
            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = REC_CREATE_TIME;//type_inq == "1" ? REC_CREATE_TIME : DateTime.Now.ToString("yyyyMMdd") + "000000";


            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = REC_REVISE_TIME;




            EF.DSUtility.PageInfoDataTable PageInfo = new EF.DSUtility.PageInfoDataTable();
            PageInfo.AddPageInfoRow((int.Parse(page_no) - 1) * int.Parse(page_size), int.Parse(page_size));

            inBlock.Tables.Add("PageInfo"); //后台需要
            inBlock.Tables["PageInfo"].Merge(PageInfo);

            string svr = "pmnmym00_inq"; ////高级查询
            


            EI.EIInfo outBlock = EI.EITuxedo.CallService(svr, inBlock);



            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("MM10");
            DataTable dtTemp = null;

             

            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                string BILL_STATUS_DESC = outBlock.Tables[0].Rows[i]["BILL_STATUS_DESC"].ToString().Trim();
                if (BILL_STATUS_DESC.Trim() != "")
                {
                    if (outBlock_TEMP.Tables[0].Select("CODE  = '" + BILL_STATUS_DESC + "'").Count<DataRow>() == 1)
                    {
                        dtTemp = outBlock_TEMP.Tables[0].Select("CODE  = '" + BILL_STATUS_DESC + "'").CopyToDataTable<DataRow>();
                        outBlock.Tables[0].Rows[i]["BILL_STATUS_DESC"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                    }
                }

            }

            outBlock.Tables[0].TableName = "Table0";









            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


       


        [HttpPost]
        [Route("PostYm00inq_Load")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostYm00inq_Load([FromBody]  JObject obj)
        {

            //小代码转换；
            EI.EIInfo outBlock = EF.Utility.GetCodeClassValue("MM10");

            DataRow dr1 = outBlock.Tables["MM10"].NewRow();
            dr1["CODE"] = " ";
            dr1["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["MM10"].Rows.InsertAt(dr1, 0);


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        [HttpPost]
        [Route("PostYm00inq_Save")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostYm00inq_Save([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");


            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();

            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
            string OUT_STOCK_NO = obj["OUT_STOCK_NO"] == null ? " " : obj["OUT_STOCK_NO"].ToString();
            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();


            string svr = "pmmm02n_pro";

            EI.EIInfo inBlock = new EI.EIInfo();
            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.company_name = "常熟市龙特耐磨球有限公司";

            inBlock.sys_info.userid = USERID;
            inBlock.sys_info.username = USERNAME;
            inBlock.sys_info.formname = FORMNAME;


            //销售出库
            if (FORMNAME == "XS")
            {
                inBlock.Tables[0].Columns.Add("OUT_STOCK_NO");
                inBlock.Tables[0].Rows.Add(OUT_STOCK_NO);

                if (PRO_TYPE.ToUpper() == "DEL") //
                {
                    svr = "mm0020_del";
                }
                else if (PRO_TYPE.ToUpper() == "PAA") //销售确认
                {
                    inBlock.Tables[0].TableName = "PE19";
                }
                else if (PRO_TYPE.ToUpper() == "PBB") //销售回退
                {
                    inBlock.Tables[0].TableName = "PF19";
                }

            }
            //调拨出
            else if (FORMNAME == "DBO")
            {
                inBlock.Tables[0].Columns.Add("MOVE_STOCK_APPLY_NO");
                inBlock.Tables[0].Rows.Add(OUT_STOCK_NO);

                if (PRO_TYPE.ToUpper() == "DEL") //
                {
                    svr = "mm0031_del";
                }
                else if (PRO_TYPE.ToUpper() == "PAA") //调拨出确认
                {
                    inBlock.Tables[0].TableName = "PA31";
                }
                else if (PRO_TYPE.ToUpper() == "PBB") //调拨出回退
                {
                    inBlock.Tables[0].TableName = "PB31";
                }
            }
            //调拨入
            else if (FORMNAME == "DBI")
            {
                inBlock.Tables[0].Columns.Add("MOVE_STOCK_NO");
                inBlock.Tables[0].Rows.Add(OUT_STOCK_NO);

                if (PRO_TYPE.ToUpper() == "DEL") //
                {
                    svr = "mm0034_del";
                }
                else if (PRO_TYPE.ToUpper() == "PAA") //调拨出确认
                {
                    inBlock.Tables[0].TableName = "PC19";
                }
                else if (PRO_TYPE.ToUpper() == "PBB") //调拨出回退
                {
                    inBlock.Tables[0].TableName = "PD19";
                }
            }
            else if (FORMNAME == "CZ")
            {

                inBlock.Tables[0].Columns.Add("IOSTOCK_BILL_NO");
                inBlock.Tables[0].Columns.Add("OUT_STOCK_NO");

                inBlock.Tables[0].Rows.Add(OUT_STOCK_NO, OUT_STOCK_NO);

                if (PRO_TYPE.ToUpper() == "DEL") //
                {
                    svr = "pmqmtj00_del";
                }
                else if (PRO_TYPE.ToUpper() == "PAA") //处置出库确认
                {
                    inBlock.Tables[0].TableName = "PA18";
                }
                else if (PRO_TYPE.ToUpper() == "PBB") //处置出库确认回退
                {
                    inBlock.Tables[0].TableName = "PB18";
                }
            }
            //退库
            else if (FORMNAME == "SR")
            {
                inBlock.Tables[0].Columns.Add("MOVE_STOCK_NO");
                inBlock.Tables[0].Columns.Add("OUT_STOCK_NO");

                inBlock.Tables[0].Rows.Add(OUT_STOCK_NO, OUT_STOCK_NO);

                if (PRO_TYPE.ToUpper() == "DEL") //
                {
                    svr = "mm0034_del";
                }
                else if (PRO_TYPE.ToUpper() == "PAA") //
                {
                    inBlock.Tables[0].TableName = "PA91";
                }
                else if (PRO_TYPE.ToUpper() == "PBB") //退库
                {
                    inBlock.Tables[0].TableName = "PB91";
                }
            }

            else if (FORMNAME == "SR2")
            {
                inBlock.Tables[0].Columns.Add("MOVE_STOCK_NO");
                inBlock.Tables[0].Columns.Add("OUT_STOCK_NO");

                inBlock.Tables[0].Rows.Add(OUT_STOCK_NO, OUT_STOCK_NO);

                if (PRO_TYPE.ToUpper() == "DEL") //
                {
                    svr = "mm0034_del";
                }
                else if (PRO_TYPE.ToUpper() == "PAA") //
                {
                    inBlock.Tables[0].TableName = "PA92";
                }
                else if (PRO_TYPE.ToUpper() == "PBB") //退库
                {
                    inBlock.Tables[0].TableName = "PB92";
                }
            }


            else
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "无此业务类型FORMNAME["+ FORMNAME+ "]");
                return outBlock;
            }



            // 调用后台services保存信息 //调拨出库；
            EI.EIInfo outBlock2 = EI.EITuxedo.CallService(svr, inBlock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }

            


        }
    }
}